16. Exercise: Importance

L1 A16 Importance

When we first created the notification in Step 1.6, the egg timer is set to send notifications with low priority since it was designed to not disturb the user with notifications. However, it might be a good idea to get the attention of the user before the egg overcooks. In order to change the importance level of the notification, let’s start with the channel settings. Channel importance affects the interruption level of all notifications posted in that channel, and must be specified in the NotificationChannel constructor.

Exercise

  1. In order to change the importance level of your app’s notification channel, open EggTimerFragment.kt and navigate to createChannel(). Change the importance level from IMPORTANCE_LOW to IMPORTANCE_HIGH. Changing this will change the behavior of your notification to make a sound and appear as a heads-up notification even when the app is in the foreground.
// EggTimerFragment.kt

    val notificationChannel = NotificationChannel(
        channelId,
        channelName,
        // TODO: Step 2.4 change importance
        NotificationManager.IMPORTANCE_HIGH
    )
  1. To support devices running API level 25 or lower, you must also call setPriority() for each notification, using a constant from the NotificationCompat class. To fix this, open NotificationUtils.kt and add PRIORITY_HIGH to notification builder object.
// NotificationUtils.kt
   .addAction(
       R.drawable.common_google_signin_btn_icon_dark,
       applicationContext.getString(R.string.snooze),
       snoozePendingIntent
    )
   // TODO: Step 2.5 set priority
    .setPriority(NotificationCompat.PRIORITY_HIGH)
  1. Before running the app, uninstall the app from your device or emulator to clear previous channel settings.



  2. Now run the app again and start the timer. This time when the notification is delivered you should see a popup appear at the top of the screen regardless of your app running in the foreground or background.